<?php
class Administration_GamesController extends Ehl_Controller_Admin_Action
{

    public function indexAction()
    {
        $identite = Zend_Auth::getInstance()->getIdentity();
        $options = array();
        $form = new Ehl_Form_Recherche($options, 'admingames');
        $objetModel = new Administration_Model_Games();
        $request = $this->getRequest();
        $form->populate($request->getParams());
        if ($request->isPost()) {
            $status = $form->isValidPartial($request->getPost());
        }
        $options['tout'] = (bool) $this->_getParam('tout', FALSE);
        
        //    Liste des champs qui ne sont pas dans la table mais qu'il faut intégrer dans le datagrid
        $options['champsExternes'] = array(
            '(0) as scorefirst', 
            '(0) as scoresecond', 
            '(0) as scorethird', 
            '(0) as scoreovertime', 
            '(0) as scoreshoutout'
        );
        
        $select = $objetModel->getListSelect($form->getValues(), "", $identite, $options);
        $form->setAction($this->view->smartUrl(null, null, null, array(
            'page' => (int) $this->_getParam('page', 1), 
            'tout' => $options['tout']
        )));
        $form->getElement('SelectionMesTous')->setValue($options['tout']);
        $this->view->tableSelect = $select;
        $this->view->tableHeader = array(
            'id' => __('id'), 
            'idTeamHome' => __('Home'), 
            'idTeamVisitor' => __('Visitor'), 
            'idLocation' => __('Location'), 
            'date' => __('Date'), 
            'scorefirst' => __('1'), 
            'scoresecond' => __('2'), 
            'scorethird' => __('3'), 
            'scoreovertime' => __('OT'), 
            'scoreshoutout' => __('SH'), 
            'creationUser' => array(
                'name' => __('created by'), 
                'visible' => $options['tout'], 
                'search' => $options['tout']
            ), 
            'creationDate' => __('création')
        );
        $this->view->tableOptions = array(
            'callback' => array(
                $this, 
                '_transformRowsCallback'
            ), 
            'link' => array(
                'action' => 'voir'
            ), 
            'pageNumber' => (int) $this->_getParam('page', 1), 
            'pageItemPerPage' => (int) $this->_getParam('par', 10), 
            'form' => $form
        );
        $this->view->form = $form;
    }

    public function editerAction()
    {
        $request = $this->getRequest();
        
        $teams = new Administration_Model_Teams();
        $liste = $teams->listerPourSelect("name", true);
        $params['teams'] = $liste;
        
        $locations = new Administration_Model_Locations();
        $liste = $locations->listerPourSelect("name", true);
        $params['locations'] = $liste;
        
        $form = new Administration_Form_Games($params);
        if ($request->isPost()) {
            if ($form->isValid($request->getPost())) {
                $model = new Administration_Model_Games($form->getValues());
                $model->sauver();
                return $this->_helper->redirector('index');
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id != 0) {
                $model = new Administration_Model_Games();
                $model->chercher($id);
                $form->populate($model->toArray());
            }
        }
        $this->view->form = $form;
    }

    public function voirAction()
    {
        
        $request = $this->getRequest();
        if ($this->getRequest()->isGet()) {
            $id = $this->_getParam('id', 0);
            $model = new Administration_Model_Games($id);
            
            $teams = new Administration_Model_Teams();
            $liste = $teams->listerPourSelect("name", true);
            
            $form = new Administration_Form_Games($liste, "Voir");
            $form->populate($model->toArray());
            $this->view->form = $form;
            $this->view->model = $model;
        }
    }

    /* (non-PHPdoc)
     * @see Jnd_Controller_Action::_transformRowsCallback()
     */
    public function _transformRowsCallback( $row, $view )
    {
        if (isset($row['born']) && $row['born'] != "") {
            $row['born'] = date(substr($row['born'], 0, strpos($row['born'], " ")));
        }
        
        $row['scorefirst'] = $row['scoreHomeFirst'] . " - " . $row['scoreVisitorFirst'];
        $row['scoresecond'] = $row['scoreHomeSecond'] . " - " . $row['scoreVisitorSecond'];
        $row['scorethird'] = $row['scoreHomeThird'] . " - " . $row['scoreVisitorThird'];
        $row['scoreovertime'] = $row['scoreHomeOvertime'] . " - " . $row['scoreVisitorOvertime'];
        $row['scoreshoutout'] = $row['scoreHomeShoutout'] . " - " . $row['scoreVisitorShoutout'];
    }

}

